/**
 * 
 */
package org.gov.vis.data.controller;

import java.util.List;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.common.exceptions.SystemException;
import org.gov.vis.data.dao.ProcurementSemenDao;
import org.gov.vis.data.dao.ProcurementSemenDaoImpl;
import org.gov.vis.data.dto.ProcurementSemenDto;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author user
 *
 */
public class ProcurementSemenCtlrImpl implements ProcurementSemenCtlr {

	/**
	 * Method called to Add or Edit the ProcurementSemen
	 * @param prSemenDto
	 * @throws Exception
	 */

	public void addOrEditPrSemen(final ProcurementSemenDto _prSemenDto) throws Exception {
		try{
			/*
			 * Below set of validations check for Required/Mandatory fields
			 */
			String _prSemenCodeErr = ValidatorUtils.validateString(rb.getString("dialog_label_prmt_semen_code"), _prSemenDto.getCode(), 1, 20, true);
			if(_prSemenCodeErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_semen_code").toUpperCase(), _prSemenCodeErr);

			
			String _prSemenBullNoErr = ValidatorUtils.validateString(rb.getString("dialog_label_prmt_semen_bullno"), _prSemenDto.getBullNo(), 1, 20, true);
			if(_prSemenBullNoErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_semen_bullno").toUpperCase(), _prSemenBullNoErr);

			String _prSemenFromStnErr = ValidatorUtils.validateString(rb.getString("dialog_label_prmt_semen_frmstation"), _prSemenDto.getFrmStation(), 4, 50, true);
			if(_prSemenFromStnErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_semen_frmstation").toUpperCase(), _prSemenFromStnErr);

			/*
			 * Below are NOT required fields but are validated on their data type 
			 */
			String _prSemenNoOfStrawsErr = ValidatorUtils.validateOnlyNumber(rb.getString("dialog_label_prmt_semen_noofStrwas"), _prSemenDto.getNoStraws(), 0, Short.MAX_VALUE, false, false);
			if(_prSemenNoOfStrawsErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_semen_noofStrwas").toUpperCase(), _prSemenNoOfStrawsErr);
			String _prSemenDORErr = ValidatorUtils.validateString(rb.getString("dialog_label_prmt_semen_dtofrcpt"), DU.format(_prSemenDto.getDoRcpt(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(_prSemenDORErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_semen_dtofrcpt").toUpperCase(), _prSemenDORErr);

			
			
			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			
			/*  Below set of validations check for duplicate records
			 */

		//check if there exists anprSemen PrSemen Object with the same Code
		//If Code has been duplicated, then throw the alert
			ProcurementSemenDto prSemenDto = null;
		if(_prSemenDto.getPrSemenId() > 0){
			//get the PrSemenDto on the passed Code name
			prSemenDto = this.getPrSemenOnCode(_prSemenDto.getCode());
			if(prSemenDto != null && prSemenDto.getPrSemenId() != _prSemenDto.getPrSemenId()){
				errorMsgs.put(rb.getString("dialog_label_prmt_semen_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_PROCUREMENTSEMEN_MSG));
			}
		}//end if
		else{
			//get the PrSemenDto on the passed Code name
			prSemenDto = this.getPrSemenOnCode(_prSemenDto.getCode());
			if(prSemenDto != null){
				errorMsgs.put(rb.getString("dialog_label_prmt_semen_code").toUpperCase(), rb.getString(GuiConstants.DATA_DUPLICATE_PROCUREMENTSEMEN_MSG));
			}
		}//end else
			 	
			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			final ProcurementSemenDao _prSemenDao = new ProcurementSemenDaoImpl();
			//add or edit the Procurement Semen Object
			_prSemenDao.addOrEditProcurementSemen(_prSemenDto);
		}
		catch(InvalidInputException ie){
			throw ie;
		}
		catch (Exception ex) {
			if(_prSemenDto.getPrSemenId() > 0){
				throw new SystemException(rb.getString(GuiConstants.DATA_EDIT_PROCUREMENTSEMEN_EXCEPTION));
			}
			else{
				throw new SystemException(rb.getString(GuiConstants.DATA_ADD_PROCUREMENTSEMEN_EXCEPTION));
			}
		}
	}//end addOrEditPrSemen

	/**
	 * Method called to delete the ProcurementSemen Object based on the passed prSemen Id
	 * @param _prSemenId
	 * @throws Exception
	 */

	public void deletePrSemen(final long _prSemenId) throws Exception {
		try{
			final ProcurementSemenDao _prSemenDao = new ProcurementSemenDaoImpl();
			_prSemenDao.deleteProcurementSemen(_prSemenId);
		}
		catch (Exception ex) {
			
			throw new SystemException(rb.getString(GuiConstants.DATA_DELETE_PROCUREMENTSEMEN_EXCEPTION));
		}
	}//end deletePrSemen

	/**
	 * Method called to get the ProcurementSemen based on the passed primary key Id
	 * @param _prSemenId
	 * @return
	 * @throws Exception
	 */

	public ProcurementSemenDto getPrSemen(final long _prSemenId) throws Exception {
		try{
			final ProcurementSemenDao _prSemenDao = new ProcurementSemenDaoImpl();
			ProcurementSemenDto _prSemenDto = new ProcurementSemenDto();
			_prSemenDto.setPrSemenId(_prSemenId);

			ProcurementSemenDto prSemenDto = null;
			List<ProcurementSemenDto> _prSemenList = _prSemenDao.getProcurementSemens(_prSemenDto);
			if(_prSemenList != null && !_prSemenList.isEmpty()){
				prSemenDto = _prSemenList.get(0);
			}

			return prSemenDto;
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_VIEW_EDIT_PROCUREMENTSEMEN_EXCEPTION));
		}
	}//end getPrSemen


	/**
	 * Method called to get the ProcurementSemen based on the passed Breed
	 * @param _prSemenCode
	 * @return
	 * @throws Exception
	 */

	public ProcurementSemenDto getPrSemenOnBreed(final String _breed) throws Exception {
		final ProcurementSemenDao _prSemenDao = new ProcurementSemenDaoImpl();
		ProcurementSemenDto _prSemenDto = new ProcurementSemenDto();
		_prSemenDto.setBreed(_breed);

		ProcurementSemenDto prSemenDto = null;
		List<ProcurementSemenDto> _prSemenList = _prSemenDao.getProcurementSemens(_prSemenDto);
		if(_prSemenList != null && !_prSemenList.isEmpty()){
			prSemenDto = _prSemenList.get(0);
		}

		return prSemenDto;
	}//end getPrSemenOnBreed

	/**
	 * Method called to get the ProcurementSemen based on the passed BullNo
	 * @param _prSemenCode
	 * @return
	 * @throws Exception
	 */

	public ProcurementSemenDto getPrSemenOnBullNo(final String _bullNo) throws Exception {
		final ProcurementSemenDao _prSemenDao = new ProcurementSemenDaoImpl();
		ProcurementSemenDto _prSemenDto = new ProcurementSemenDto();
		_prSemenDto.setBullNo(_bullNo);

		ProcurementSemenDto prSemenDto = null;
		List<ProcurementSemenDto> _prSemenList = _prSemenDao.getProcurementSemens(_prSemenDto);
		if(_prSemenList != null && !_prSemenList.isEmpty()){
			prSemenDto = _prSemenList.get(0);
		}

		return prSemenDto;
	}//end getPrSemenOnBullNo

	/**
	 * Method called to get the ProcurementSemen based on the passed PrSemen Object
	 * @param prSemenDto
	 * @return
	 * @throws Exception
	 */

	public ProcurementSemenDto getPrSemen(final ProcurementSemenDto _prSemenDto) throws Exception {
		final ProcurementSemenDao _prSemenDao = new ProcurementSemenDaoImpl();

		ProcurementSemenDto prSemenDto = null;
		List<ProcurementSemenDto> _prSemenList = _prSemenDao.getProcurementSemens(_prSemenDto);
		if(_prSemenList != null && !_prSemenList.isEmpty()){
			prSemenDto = _prSemenList.get(0);
		}

		return prSemenDto;
	}//end getPrSemens

	/**
	 * Method called to get the list of ProcurementSemen based on the passed PrSemen Object
	 * @param prSemenDto
	 * @return
	 * @throws Exception
	 */

	public List<ProcurementSemenDto> getPrSemens(final ProcurementSemenDto _prSemenDto) throws Exception {
		final ProcurementSemenDao _prSemenDao = new ProcurementSemenDaoImpl();
		return _prSemenDao.getProcurementSemens(_prSemenDto);
	}//end getPrSemens

	/**
	 * Method called to search for the ProcurementSemens based on the passed PrSemen Object
	 * @param prSemenDto
	 * @return
	 * @throws Exception
	 */

	public List<ProcurementSemenDto> searchPrSemens(ProcurementSemenDto _prSemenDto) throws Exception {
		try{
			final ProcurementSemenDao _prSemenDao = new ProcurementSemenDaoImpl();
			return _prSemenDao.searchProcurementSemens(_prSemenDto);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.DATA_VIEW_MANTAIN_PROCUREMENTSEMEN_EXCEPTION));
		}
	}//end searchPrSemens
	
	/**
	 * Method called to get the AI based on the passed Code
	 * @param _aiCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProcurementSemenDto getPrSemenOnCode(final String _code) throws Exception {
		final ProcurementSemenDao _prSemenDao = new ProcurementSemenDaoImpl();
		ProcurementSemenDto _prSemenDto = new ProcurementSemenDto();
		_prSemenDto.setCode(_code);

		ProcurementSemenDto prSemenDto = null;
		List<ProcurementSemenDto> _prSemenList = _prSemenDao.getProcurementSemens(_prSemenDto);
		if(_prSemenList != null && !_prSemenList.isEmpty()){
			prSemenDto = _prSemenList.get(0);
		}

		return prSemenDto;
	}//end getAIOnCode


}//end class
